//
//  TVCellOfFirstRowListenFM.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "TVCellOfFirstRowListenFM.h"
#import "BaseCollectionView.h"
#import "CVCellOfFirstRowListenFM.h"
#import "ZWYNetTool.h"
#import "ListenFMRootClass.h"
#import "ListenFMData.h"
#import "ListenFMCategory.h"
#import "ListenFMHot_Fm.h"
#import "ListenFMNew_Fm.h"
#import "ListenFMNew_Lesson.h"
#import "UIImageView+WebCache.h"
#import "VCOfListenFMPlayInterface.h"
#import "VCOfListenFM.h"
#import "ClickButtonInListenFMRootClass.h"
#import "ClickButtonInListenFMData.h"
@interface TVCellOfFirstRowListenFM()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collectionViewOfFirstRowListenFM;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) ListenFMRootClass *listenFMRootClass;
@property (nonatomic, strong) ClickButtonInListenFMRootClass *clickButtonInListenFMRootClassOfHot;
@property (nonatomic, strong) NSMutableArray<ClickButtonInListenFMData *> *mArrayOfClickButtonInListenFMData;
@property (nonatomic, assign) int numberOfOffect;
@end
@implementation TVCellOfFirstRowListenFM
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
         _numberOfOffect = 10;
        // 解析数据
        [self handleData];
        // 创建 Collectionview
        [self createCollectionView];
    }
    return self;
}
#pragma mark - 数据解析
- (void)handleData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/fm-home-list.json" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _listenFMRootClass = [ListenFMRootClass modelDic:result];
        [self.collectionViewOfFirstRowListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=hot&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f53c5d99180cb011e312dcf3f300733d&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        //        _clickButtonInListenFMRootClassOfHOt = []
        _clickButtonInListenFMRootClassOfHot = [ClickButtonInListenFMRootClass modelDic:result];
        _mArrayOfClickButtonInListenFMData = _clickButtonInListenFMRootClassOfHot.data.mutableCopy;
   [self.collectionViewOfFirstRowListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    //
    [self keepHandleData];
    

}
#pragma mark - 
- (void)keepHandleData{
    [ZWYNetTool GET:[NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=hot&offset=%d&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f53c5d99180cb011e312dcf3f300733d&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93", _numberOfOffect] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        //        _clickButtonInListenFMRootClassOfHOt = []
        if ([result[@"data"] lastObject][@"title"]) {
            NSLog(@"存在");
            static int i = 0;
            i++;
            NSLog(@"%d", i);
            NSLog(@"%@", [result[@"data"] lastObject][@"title"]);
            _clickButtonInListenFMRootClassOfHot = [ClickButtonInListenFMRootClass modelDic:result];
            for (ClickButtonInListenFMData *clickButtonInListenFMData in _clickButtonInListenFMRootClassOfHot.data) {
                [_mArrayOfClickButtonInListenFMData addObject:clickButtonInListenFMData];
                NSLog(@"%@", clickButtonInListenFMData.title);
            }
            _numberOfOffect = _numberOfOffect + 10;
            
            [self keepHandleData];
        }
        [self.collectionViewOfFirstRowListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 CollectionView
- (void)createCollectionView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionViewOfFirstRowListenFM = [[BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    self.collectionViewOfFirstRowListenFM.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionViewOfFirstRowListenFM];
    self.collectionViewOfFirstRowListenFM.delegate = self;
    self.collectionViewOfFirstRowListenFM.dataSource = self;
    [self.collectionViewOfFirstRowListenFM registerNib:[UINib nibWithNibName:@"CVCellOfFirstRowListenFM" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfFirstRowListenFM"];
    
}
#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionViewOfFirstRowListenFM.frame = self.contentView.bounds;
    self.layout.itemSize = CGSizeMake(self.contentView.bounds.size.width / 3, self.contentView.bounds.size.height);
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
#pragma mark - CollectionView 代理方法 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _listenFMRootClass.data.hot_fm.count;
}
#pragma mark - collectionView cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfFirstRowListenFM *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfFirstRowListenFM" forIndexPath:indexPath];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_listenFMRootClass.data.hot_fm[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
    cell.titleLabel.text = _listenFMRootClass.data.hot_fm[indexPath.row].title;
    return cell;
}
#pragma mark - collectionView 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    VCOfListenFMPlayInterface *vcOfListenFMPlayInterface = [VCOfListenFMPlayInterface defaultManager];
    VCOfListenFM *vcOfListenFM = (VCOfListenFM *)self.nextResponder.nextResponder.nextResponder.nextResponder;
//    vcOfListenFMPlayInterface.clickButtonInListenFMRootClass.data = _mArrayOfClickButtonInListenFMData;
    vcOfListenFMPlayInterface.mArrayOfClickButtonInListenFMData = _mArrayOfClickButtonInListenFMData;
    vcOfListenFMPlayInterface.numberOfSelectNextSong = indexPath.row + 1;
    vcOfListenFMPlayInterface.numberOfSelectLastSong = indexPath.row - 1;
    vcOfListenFMPlayInterface.urlString = _listenFMRootClass.data.hot_fm[indexPath.row].url;
    vcOfListenFMPlayInterface.titleString = _listenFMRootClass.data.hot_fm[indexPath.row].title;
    vcOfListenFMPlayInterface.speakString = _listenFMRootClass.data.hot_fm[indexPath.row].speak;
    vcOfListenFMPlayInterface.coverString = _listenFMRootClass.data.hot_fm[indexPath.row].cover;
    [vcOfListenFM presentViewController:vcOfListenFMPlayInterface animated:YES completion:^{
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
